﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using OperaHouse.Web.Models;
using OperaHouse.Business.News;

namespace OperaHouse.Web.Controllers
{
    public class NewsController : Controller
    {
        #region Fields
        private readonly INewsService _newsService;
        #endregion

        #region Ctor
        public NewsController(INewsService newsService)
        {
            this._newsService = newsService;
        }
        #endregion

        #region Action
        public ActionResult Index()
        {
            IList<NewsItemModel> newsModels = new List<NewsItemModel>();
            var news = _newsService.GetAllNewsItems();
            foreach (var item in news.ToList())
            {
                newsModels.Add(NewsItemModel.Create(item));
            }

            return View(newsModels);
        }

        public ActionResult Detail(int id)
        {
            var newsItem = _newsService.GetNewsById(id);
            if (newsItem == null)
                //No news item found with the specified id
                return RedirectToAction("Index");

            NewsItemModel model = NewsItemModel.Create(newsItem);
            return View(model);
        }
        #endregion
    }
}
